#include"threadPool.h"
#include"keyword.h"

void do_cd(int netfd,char* filename)
{
    char * rootpath = getcwd(NULL,0);
    DIR *dir = opendir(rootpath);
    struct dirent * pdir;
    while((pdir=readdir(dir))!=NULL){
        if(strncmp(pdir->d_name,filename,strlen(filename))==0)
        {
            char path_name[1024]={0};
            sprintf(path_name,"%s/%s",rootpath,filename);
            chdir(path_name);
            train_t train;


            int one=1;
            memset(&train,0,sizeof(train));
            train.length = sizeof(int);
            strncpy(train.data,(char *)&one,train.length);
            send(netfd,&train,train.length+4,0);

            memset(&train,0,sizeof(train));
            train.length = strlen(path_name);
            strncpy(train.data,path_name,train.length);
            send(netfd,&train,train.length+4,0);

            memset(rootpath,0,sizeof(rootpath));
            strncpy(rootpath,path_name,strlen(path_name));
        }
    }
    closedir(dir);
}
void do_ls(int netfd)
{

    char * rootpath = getcwd(NULL,0);
    DIR * dir = opendir(rootpath);
    struct dirent * pdir;
    char send_name[1024]={0};
    while((pdir=readdir(dir))!=NULL)
    {
        char ls_name[1024]={0};
        sprintf(ls_name,"%s   ",pdir->d_name);
        strcat(send_name,ls_name);
    }
    train_t train;
    memset(&train,0,sizeof(train));
    train.length = sizeof(int);
    int one =1;
    strncpy(train.data,(char *)&one,train.length);
    send(netfd,&train,train.length+4,0);
    memset(&train,0,sizeof(train));
    train.length = strlen(send_name);
    strncpy(train.data,send_name,train.length);
    send(netfd,&train,train.length+4,0);
    closedir(dir);

}
void do_pwd(int netfd)
{
    char fname[1024];
    memset(fname,0,sizeof(fname));
    getcwd(fname,sizeof(fname));
    send(netfd,fname,strlen(fname),0);

}

void do_rm(int netfd,char * filename)
{
    char * rootpath = getcwd(NULL,0);
    DIR * dir = opendir(rootpath);
    struct dirent * pdir;
    while((pdir=readdir(dir))!=NULL){
        if(strncmp(pdir->d_name,filename,strlen(filename))==0)
        {
            train_t train;
            memset(&train,0,sizeof(train));
            train.length = sizeof(int);
            int one =1;
            strncpy(train.data,(char *)&one,train.length);
            send(netfd,&train,train.length+4,0);

            memset(&train,0,sizeof(train));
            train.length = strlen(rootpath);
            strncpy(train.data,rootpath,train.length);
            send(netfd,&train,train.length+4,0);
            execl("rm","rm","-f","filename",NULL);
        }
    }
    closedir(dir);

}

void do_mkdir(int netfd,char * filename)
{
    char * rootpath = getcwd(NULL,0);
    char dir_name[1024]={0};
    sprintf(dir_name,"%s/%s",rootpath,filename);
    mkdir(dir_name,0777);
    train_t train;

    memset(&train,0,sizeof(train));
    train.length = sizeof(int);
    int one =1;
    strncpy(train.data,(char *)&one,train.length);
    send(netfd,&train,train.length+4,0);

    memset(&train,0,sizeof(train));
    char buf[20]="目录创建成功\n";
    train.length=strlen(buf);
    strncpy(train.data,buf,train.length);
    send(netfd,&train,4+train.length,0);
}

void do_exit(int netfd)
{
    close(netfd);
    printf("客户端%d已关闭\n",netfd);
}

